﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace RabbitMQ.WorkQueue.Receive
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var chanel = connection.CreateModel())
                {
                    chanel.QueueDeclare(queue: "task_queue", durable: true, exclusive: false, autoDelete: false, arguments: null);
                    chanel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
                    var consumer = new EventingBasicConsumer(chanel);
                    consumer.Received += (model, ea) =>
                    {
                        var messsage = Encoding.UTF8.GetString(ea.Body);
                        Console.WriteLine("[x] received {0}", messsage);

                        int dots = messsage.Split('.').Length - 1;
                        Thread.Sleep(dots * 1000);
                        Console.WriteLine("[x] done.");
                        chanel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                    };
                    chanel.BasicConsume("task_queue", autoAck: false, consumer: consumer);
                    Console.ReadKey();
                }
            }
        }
    }
}
